package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionEnterpriseService;
import com.bidevalution.business.service.FileCompareService;
import com.bidevalution.controller.dto.SimilarEnterpriseDto;
import com.bidevalution.controller.util.RequestUtil;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.FileCompareResult;
import com.bidevalution.support.context.ResponseObject;
import com.bidevalution.support.context.WebSocketOpTypeEnum;
import com.bidevalution.support.websocket.WebSocketCached;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文件对比控制层
 * Created by glodon on 2019-08-27.
 */
@Log4j2
@RestController
@RequestMapping("/fileCompare")
@Api(tags = "文档对比")
public class FileCompareController extends BaseController{

    @Value("${download.file.path}")
    private String downloadFileUrl;

    @Autowired
    private FileCompareService fileCompareService;

    @Autowired
    private BeSectionEnterpriseService beSectionEnterpriseService;

    @GetMapping("/getSimilarEnterprise")
    @ApiOperation(value = "根据条件获取与其他企业的技术标文件对比结果", response = SimilarEnterpriseDto.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "enterpriseId", value = "企业id", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "methodId", value = "评审内容id"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "similar", value = "相似度,格式：百分数，如：60%"),
            })
    public Response<List<SimilarEnterpriseDto>> getSimilarEnterprise(@RequestParam("sectionId") String sectionId, @RequestParam("enterpriseId") String enterpriseId,
                                                                     @RequestParam(value = "methodId", required = false) String methodId, @RequestParam(value = "similar",required = false) String similar) {
        try {
            List<FileCompareResult> fileCompareResultList = fileCompareService.getSimilarEnterprise(sectionId,enterpriseId,methodId,similar);
            List<SimilarEnterpriseDto> similarEnterpriseDtoList = new ArrayList<>();
            if(fileCompareResultList != null && fileCompareResultList.size() >0){
                SimilarEnterpriseDto similarEnterpriseDto = null;
                for(FileCompareResult fileCompareResult : fileCompareResultList) {
                    similarEnterpriseDto = new SimilarEnterpriseDto();
                    if(enterpriseId.equals(fileCompareResult.getEnterpriseId1())){
                        similarEnterpriseDto.setEnterpriseId(fileCompareResult.getEnterpriseId2());
                        similarEnterpriseDto.setEnterpriseName(fileCompareResult.getEnterpriseName2());
                    } else if(enterpriseId.equals(fileCompareResult.getEnterpriseId2())) {
                        similarEnterpriseDto.setEnterpriseId(fileCompareResult.getEnterpriseId1());
                        similarEnterpriseDto.setEnterpriseName(fileCompareResult.getEnterpriseName1());
                    }
                    NumberFormat percentFormat =java.text.NumberFormat.getPercentInstance();
                    similarEnterpriseDto.setSimilar(percentFormat.format(fileCompareResult.getSimilar()));
                    similarEnterpriseDtoList.add(similarEnterpriseDto);
                }
            }
           return Response.ok(similarEnterpriseDtoList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据条件获取与其他企业的技术标文件对比结果失败");
        }
    }

    @GetMapping("/getSectionEnterprises")
    @ApiOperation(value = "根据标段id获取企业列表", response = BeSectionEnterprise.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true)
            })
    public Response<List<BeSectionEnterprise>> getSectionEnterprises(@RequestParam("sectionId") String sectionId) {
        try {
            return Response.ok(beSectionEnterpriseService.getBeSectionEnterpriseList(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据标段id获取企业列表失败");
        }
    }

    @GetMapping("/getEvalContentList")
    @ApiOperation(value = "根据标段id获取评审内容列表", response = BeSectionEnterprise.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true)
            })
    public Response<List<Map<String, String>>> getEvalContentList(@RequestParam("sectionId") String sectionId) {
        try {
            return Response.ok(fileCompareService.getEvalContentList(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据标段id获取评审内容列表失败");
        }
    }

    @GetMapping("/getTechnologyPdf")
    @ApiOperation(value = "根据标段id和评审内容id获取技术标文件", response = String.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "methodId", value = "评审内容id", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "enterpriseId", value = "企业id", required = true)
            })
    public Response<String> getTechnologyPdf(@RequestParam("sectionId") String sectionId, @RequestParam(value = "methodId") String methodId, @RequestParam("enterpriseId") String enterpriseId) {
        try {
            String pdfId = fileCompareService.getTechnologyPdf(sectionId, methodId, enterpriseId);
            return Response.ok(downloadFileUrl + "/UploadedFile/" + pdfId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据标段id和评审内容id获取技术标文件失败");
        }
    }
}
